package com.sphd.emanu.activity.goodsfrom;

import com.google.gson.JsonObject;
import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.goods.Commodity;
import com.sphd.emanu.pojo.goods.Composition;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.JsonUtil;
import com.wzutils.L;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class GoodsfromPresenter extends BasePresenterImpl<GoodsfromContract.View> implements GoodsfromContract.Presenter {

    @Override
    public void updateCommoditySrcCome(int id, String source, String composition, int categoryId, int oid, final SweetAlertDialog sweetAlertDialog) {
        emanuService.updateCommoditySrcCome(id, source, composition, categoryId, oid).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString());
                mView.setUpRes(res(response),sweetAlertDialog);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void getFromList(int id) {
        Call<JsonObject> call = emanuService.selectComposition(id);
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString());
                Commodity commodity =GsonUtil.GsonToBean(response.body().toString(),Commodity.class);
               mView.setEntity(commodity);

            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void deleteComposition(int id, final SweetAlertDialog sweetAlertDialog, final int position) {
        Call<JsonObject> call=emanuService.deleteComposition(id);
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                int res=response.body().get("resStatus").getAsInt();
                mView.setDelRes(res,sweetAlertDialog,position);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
            Error(t);
            }
        });
    }
}
